#include "OSEnv.h"
#include <ShlObj.h>

COSEnv::COSEnv(void)
{
}

COSEnv::~COSEnv(void)
{
}

BOOL COSEnv::ShutDown( BOOL bForce )
{
    return ExitWindowsEx(bForce ? EWX_SHUTDOWN | EWX_FORCE : EWX_SHUTDOWN, 0);
}

BOOL COSEnv::Logoff( BOOL bForce )
{
    return ExitWindowsEx(bForce ? EWX_LOGOFF | EWX_FORCE : EWX_LOGOFF, 0);
}

BOOL COSEnv::Reboot( BOOL bForce )
{
    return ExitWindowsEx(bForce ? EWX_REBOOT | EWX_FORCE : EWX_REBOOT, 0);
}

void COSEnv::GetTmpPath( tchar* szTmpPath )
{
    tchar szBuffer[MKY_DEFAULT_BUF_SIZE];
    DWORD dwRetValue = GetTempPath(MKY_DEFAULT_BUF_SIZE, szBuffer);
    if (dwRetValue > MKY_DEFAULT_BUF_SIZE || dwRetValue == 0)
    {
        _tcscpy(szTmpPath, _T("C:\\temp"));
    }
    else
    {
        _tcscpy(szTmpPath, szBuffer);
    }
}

unsigned int COSEnv::GetProcessorCount()
{
    SYSTEM_INFO systemInfo;
    memset(&systemInfo, 0, sizeof(SYSTEM_INFO));
    GetSystemInfo(&systemInfo);
    return (unsigned int)systemInfo.dwNumberOfProcessors;
}

DWORD COSEnv::GetTotalMemoryPerBytes()
{
    MEMORYSTATUS memStatus;
    GlobalMemoryStatus(&memStatus);
    return memStatus.dwTotalPhys;
}

DWORD COSEnv::GetCurUseMemoryPerBytes()
{
    return 0;
}

BOOL COSEnv::RegisterComSrv( const tchar* szFileName, bool bInstall )
{
    ASSERT(szFileName != NULL);
    if (szFileName == NULL)
    {
        return FALSE;
    }

    HMODULE hMod = LoadLibrary(szFileName);
    if (NULL == hMod)
    {
        return FALSE;
    }

    BOOL bRet = FALSE;
    typedef void (*Fun)();

    Fun fun = (Fun)GetProcAddress(hMod, bInstall ? _T("DllRegisterServer") : _T("DllUnregisterServer"));
    if (NULL != fun)
    {
        bRet = TRUE;
        (*fun)();
    }

    FreeLibrary(hMod);

    return bRet;
}

void COSEnv::GetProgramFilesDir( tchar* szPath )
{
    SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, szPath);
}
